package com.uhylab.uhuvinetest.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.Toast;

import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.base.UHBaseActivity;
import com.uhylab.uhuvinetest.utils.UnzipAssets;
import com.uhylab.uhuvinetest.utils.sharedpreutils.SharedPreUtils;

import java.io.IOException;

/**
 * Created by xyb on 15/12/8 at UHylab
 *
 * @version 1.0
 */
public class UHStartActivity extends UHBaseActivity implements Animation.AnimationListener {
    private ImageView start_img;

    @Override
    public int bindLayout() {
        return R.layout.activity_start;
    }

    @Override
    public void initView(View view) {
        start_img = (ImageView) view.findViewById(R.id.app_start_img);
    }

    @Override
    public void doBusiness(Context mContext) {
        boolean zip = SharedPreUtils.getBoolean(this, "ZIP", false);
        if (!zip) {
            try {
                String path = getExternalFilesDir(null).getPath();
                Log.e(TAG, path);
                UnzipAssets.unZip(this, "Uhylab.zip", path, true);
                SharedPreUtils.setBoolean(this,"ZIP",true);
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(this, "请检查写入权限", Toast.LENGTH_SHORT).show();
                finish();
                return;
            }

        }

        AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 1f);//渐变动画
        alphaAnimation.setDuration(2000);// 设置动画的时间
        alphaAnimation.setFillAfter(true);// 保持动画状态
        start_img.setAnimation(alphaAnimation);
        alphaAnimation.setAnimationListener(this);// 设置动画监听
    }

    @Override
    public void resume() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void onAnimationStart(Animation animation) {

    }

    // 动画完成监听
    @Override
    public void onAnimationEnd(Animation animation) {
        jumpNextPage();

    }

    private void jumpNextPage() {
        boolean first = SharedPreUtils.getBoolean(getContext(), "is_first_open", false);
        if (!first) {
            Intent intent = new Intent(getContext(), UHGuideActivity.class);
            this.startActivity(intent);
            finish();
        } else {
            Intent intent = new Intent(this, UHMainActivity.class);
            this.startActivity(intent);
            finish();
        }
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}
